/**
 * Git 触发器管理相关 API
 */

import { get, post, put, del } from '@/utils/request'
import { TriggerType } from '@/types/enums'

/**
 * Git 触发器信息
 */
export interface GitTrigger {
  id: number
  workflowId: number
  name: string
  triggerType: TriggerType
  branchPattern: string | null
  tagPattern: string | null
  pathFilter: string | null
  isEnabled: boolean
  createdAt: string
  updatedAt: string
}

/**
 * 创建触发器请求
 */
export interface CreateGitTriggerRequest {
  workflowId: number
  name: string
  triggerType: TriggerType
  branchPattern?: string
  tagPattern?: string
  pathFilter?: string
}

/**
 * 更新触发器请求
 */
export interface UpdateGitTriggerRequest {
  name: string
  triggerType: TriggerType
  branchPattern?: string
  tagPattern?: string
  pathFilter?: string
  isEnabled: boolean
}

/**
 * 创建触发器规则
 */
export function createGitTrigger(data: CreateGitTriggerRequest): Promise<GitTrigger> {
  return post<GitTrigger>('/api/GitTrigger', data)
}

/**
 * 更新触发器规则
 */
export function updateGitTrigger(
  id: number,
  data: UpdateGitTriggerRequest
): Promise<GitTrigger> {
  return put<GitTrigger>(`/api/GitTrigger/${id}`, data)
}

/**
 * 删除触发器规则
 */
export function deleteGitTrigger(id: number): Promise<void> {
  return del<void>(`/api/GitTrigger/${id}`)
}

/**
 * 获取工作流的触发器列表
 */
export function getWorkflowTriggers(workflowId: number): Promise<GitTrigger[]> {
  return get<GitTrigger[]>(`/api/GitTrigger/workflow/${workflowId}`)
}

/**
 * 获取触发器详情
 */
export function getGitTriggerById(id: number): Promise<GitTrigger> {
  return get<GitTrigger>(`/api/GitTrigger/${id}`)
}
